﻿SSVC.Wrap.Dojo.SetObject("SSVC.SVG.ActiveScribbleElement",
	SSVC.Wrap.Dojo.Declare("ActiveScribbleElement", SSVC.SVG.ActiveElement,
	{
		currentPoint: null,
		pointList: null,

		constructor: function()
		{
			this.currentPoint = 0;
		},

		Draw: function(startX, startY, currX, currY, shiftKey)
		{
			this.SelectMode = this.SelectOptions.None;
			if (this.currentPoint === 0)
			{
				this.X = startX;
				this.Y = startY;

				this.InnerElement.setPoint(this.currentPoint, startX, startY);
				this.currentPoint++;
				return;
			}

			this.InnerElement.setPoint(this.currentPoint, currX, currY);
			this.currentPoint++;
		},

		_refreshSelection: function()
		{
			var lineWidth = (this.InnerElement.LineWidth) ? this.InnerElement.LineWidth : 2;
			this.Frame.update(this.X - lineWidth, this.Y - lineWidth, this.OX + lineWidth * 2, this.OY + lineWidth * 2);

			switch (this.SelectMode)
			{
				case this.SelectOptions.Contoured:
					break;
				case this.SelectOptions.Selected:
					this._showSelection();
					break;
				case this.SelectOptions.None:
					this._hideSelection();
					break;
			}
		},

		_move: function(x, y)
		{
			return;
			//if (this.selectedPoint == null) return;

			//var deltaX = x - this.selectedPoint.x;
			//var deltaY = y - this.selectedPoint.y;

			//this.selectedPoint.x = x;
			//this.selectedPoint.y = y;

			////this.X += deltaX;
			////this.Y += deltaY;
			////this.Node.setAttribute("transform", "rotate({0} {1} {2})".Format(40, 50, 50));

			//// svg root element to access the createSVGTransform() function
			//var svgroot = this.Node.parentNode;

			//// SVGTransformList of the element that has been clicked on
			//var tfmList = this.Node.transform.baseVal;

			//// Create a seperate transform object for each transform
			//var translate = svgroot.createSVGTransform();
			//translate.setTranslate(x, y);
			////alert(evt.offsetX);
			////var rotate = svgroot.createSVGTransform();
			////rotate.setRotate(10, 0, 0);

			////var scale = svgroot.createSVGTransform();
			////scale.setScale(0.8, 0.8);

			//// apply the transformations by appending the SVGTranform objects to the SVGTransformList associated with the element
			//tfmList.appendItem(translate);
			////tfmList.appendItem(rotate);
			////tfmList.appendItem(scale);

			////var matrix = this.Node.createSVGMatrix();
			////matrix = matrix.translate(newx, newy);
			////if (this.Node.transform.baseVal.numberOfItems == 0)
			////{
			////	this.Node.transform.baseVal.createSVGTransformFromMatrix(matrix);
			////	var transformObject = this.Node.createSVGTransform();
			////	this.Node.transform.baseVal.appendItem(transformObject);
			////}
			////else
			////	this.Node.transform.baseVal.getItem(0).setMatrix(matrix);

		},

		Move: function(x, y, shiftKey)
		{
			return;
		},

		ScaleX: function(factor)
		{
			if (factor <= 0)
				return;
			for (var i in this.pointList)
			{
				this.pointList[i][0] *= factor;
			}

			this.Refresh();
		},

		ScaleY: function(factor)
		{
			if (factor <= 0)
				return;

			for (var i in this.pointList)
			{
				this.pointList[i][1] *= factor;
			}
			this.Refresh();
		},

		_setSize: function()
		{
			var mostX = 0, mostY = 0;
			for (var i in this.InnerElement.pointList)
			{
				if (mostX < this.InnerElement.pointList[i][0]) mostX = this.InnerElement.pointList[i][0];
				if (mostY < this.InnerElement.pointList[i][1]) mostY = this.InnerElement.pointList[i][1];
			}
			this.OX = mostX;
			this.OY = mostY;
		},

		_addInnerElement: function()
		{
			this.InnerElement = new SSVC.SVG.Polyline();
		},

		_addSelection: function()
		{
			this._addSelectionTops();

			this.Frame = new SSVC.SVG.Rectangle();
			this.Frame.Class = "Frame";
		},

		restoreCoo: function(xmlNode) // override for any shape
		{
			if (this.InnerElement.pointList.length === 0)
				this.InnerElement.getPoints();
		},

		Refresh: function()
		{
			this.restoreCoo();
			
			this.InnerElement.update();

			var xArr = [], yArr = [];
			if (this.pointList == null)
			{
				this.pointList = this.InnerElement.pointList;
			}
			for (var i in this.pointList)
			{
				xArr.push(this.pointList[i][0]);
				yArr.push(this.pointList[i][1]);
			}
			this.X = Math.min.apply(Math, xArr);
			this.Y = Math.min.apply(Math, yArr);
			this.OX = Math.max.apply(Math, xArr) - this.X;
			this.OY = Math.max.apply(Math, yArr) - this.Y;

			this._refreshSelection();
		}
	}));
